﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ListenMusic.Models.Reposistory.Public;
using ListenMusic.Models.Reposistory.User;
using ListenMusic.Song_ServiceReference;
using System.ServiceModel;
namespace ListenMusic.Controllers.Public
{
    public class SongController : ApplicationController
    {
        //
        // GET: /Song/
       
      
        public SongController()
        {           
        
        }
       
        //public ActionResult SearchSong_bySinger(int pageindex,int singerid)
        //{
        //    try
        //    {
        //        var files = _dowload.GetFiles();
        //        ViewBag.singerid = singerid;
        //        int pagesize = 20;
        //        int start = (pageindex - 1) * pagesize;
        //        int end = pageindex * pagesize;               
        //        if (pageindex < 0)
        //        {
        //            pageindex = 0;
        //        }
        //        List<SongSinger> lst =  _song.pagingSong_bySingerid(start, end, singerid);
        //        if (lst.Count > 0)
        //        {
        //            ViewBag.songSinger = lst;
        //            double a = _song.countSong_bySingerID(singerid) / pagesize;
        //            ViewBag.total = Math.Ceiling(a) + 1;
        //            if (Session["user"] != null)
        //            {
        //                string[] array_info_user = (string[])Session["user"];
        //                ViewBag.playList = _playlist.getPlaylist_ByMemberID(Convert.ToInt32(array_info_user[1]));
        //                ViewBag.songInPlaylist = _playlist.getSongInPlaylist(Convert.ToInt32(array_info_user[1]));
        //            }
        //            return View("SearchSongSinger");
        //        }
        //        else
        //        {
        //            return RedirectToAction("NotFound");
        //        }
        //    }
        //    catch (FaultException ex)
        //    {
        //        return View("~/Views/Shared/_Error.cshtml");
        //        throw ex;
        //    }
        //}
        public ActionResult NotFound()
        {
            return View();
        }
        public ActionResult SearchSong_byCategory(int pageindex,int categoryid)
        {
            try
            {
                var files = _dowload.GetFiles();
                int pagesize = 20;
                int start = (pageindex - 1) * pagesize ;
                int end = pageindex * pagesize;
                int count = _song.countSong_byCategoryID(categoryid);
                double numb = (double)(count / pagesize);                
                if (pageindex <= 0)
                {
                    pageindex = 1;
                }
                List<CategorySong> lst = _song.paging_getSong_byCategoryID(start, end, categoryid);
                if (lst.Count > 0)
                {
                    ViewBag.categorySong = lst;
                    ViewBag.total = Math.Ceiling(numb) + 1;
                    ViewBag.categoryID = categoryid;
                    if (Session["user"] != null)
                    {
                        string array_info_user = Session["user"].ToString();
                        ViewBag.playList = _playlist.getPlaylist_ByMemberID(array_info_user);
                        ViewBag.songInPlaylist = _playlist.getSongInPlaylist(array_info_user);
                    }
                    return View("SearchSongCategory");
                }
                else
                {
                    return RedirectToAction("NotFound");
                }
            }
            catch (FaultException ex)
            {
                return View("~/Views/Shared/_Error.cshtml");
                throw ex;
            }
        }
        public ActionResult getRange(int typeid,int countryid)
        {
            List<CountrySong> lst = _song.getRange_Song_Video(typeid,countryid);
            if (lst.Count > 0)
            {
                ViewBag.rangeSV = lst;
                if (Session["user"] != null)
                {
                    string[] array_info_user = (string[])Session["user"];
                    ViewBag.playList = _playlist.getPlaylist_ByMemberID(array_info_user[0]);
                    ViewBag.songInPlaylist = _playlist.getSongInPlaylist(array_info_user[0]);
                }
                return View("RangeSongVideo");
            }
            else
            {
                return RedirectToAction("NotFound");
            }
        }
        public ActionResult getVideo_byCategory(int categoryid)
        {
            List<ListenMusic.Video_ServiceReference.SongSinger> lst =  _video.getVideo_byCategoryID(categoryid, 2);
            if (lst.Count > 0)
            {
                ViewBag.rangeVideo = lst;
                if (Session["user"] != null)
                {
                    string[] array_info_user = (string[])Session["user"];
                    ViewBag.playList = _playlist.getPlaylist_ByMemberID(array_info_user[0]);
                    ViewBag.songInPlaylist = _playlist.getSongInPlaylist(array_info_user[0]);
                }
                return View("RangeVideo");
            }
            else
            {
                return RedirectToAction("NotFound");
            }
        }
    }
}
